package cn.zch.soft.util.thread.systemThreadScan;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class ScanAllThreadInCurrentProgram{
    @SuppressWarnings({"rawtypes","unchecked"})
    public static void main(String[] args){
        ThreadGroup group = Thread.currentThread().getThreadGroup();
        ThreadGroup parent = null;
        while((parent = group.getParent()) != null){
            group = parent;
        }
        Thread[] threads = new Thread[group.activeCount()];
        group.enumerate(threads);
        java.util.HashSet set = new java.util.HashSet();
        for(int i = 0;i < threads.length;++i){
            if(threads[i] != null && threads[i].isAlive()){
                try{
                    set.add(threads[i].getThreadGroup().getName() + "," + threads[i].getName() + "," + threads[i].getPriority());
                } catch(Throwable e){
                    e.printStackTrace();
                }
            }
        }
        String[] result = (String[])set.toArray(new String[0]);
        java.util.Arrays.sort(result);
        for(String s : result){
            System.out.println("a  " + s);
        }
        /**********************************/
        Map<Thread,StackTraceElement[]> maps = Thread.getAllStackTraces();
        Collection c = maps.keySet();
        Iterator it = c.iterator();
        while(it.hasNext()){
            System.out.println("b " + it.next());
        }
        Collection c1 = maps.keySet();
        Iterator it1 = c1.iterator();
        while(it1.hasNext()){
            System.out.println("c " + it1.next());
        }
        /**********************************/
    }
}
